﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 角色管理
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.20 
    /// </summary>
    internal class RoleService : DataFactory, RoleIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<RoleEntity>> GetList(PageInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<RoleEntity>();
                var queryParam = input.queryJson.ToObject();
                //关键字（名称、编码）
                if (!string.IsNullOrEmpty(input.keyword))
                {
                    queryWhere = queryWhere.And(m => m.FullName.Contains(input.keyword) || m.EnCode.Contains(input.keyword));
                }
                return await db.GetIQueryable<RoleEntity>().Where(queryWhere).OrderBy(t => t.SortCode).GetPageResultAsync(input);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleEntity>> GetList()
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<RoleEntity>().OrderBy(o => o.SortCode).ToListAsync();
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        public async Task<List<RoleEntity>> GetListByUserId(string userId)
        {
            using (var db = this.Basic)
            {
                var parameters = new List<DbParameter>();
                var sql = new StringBuilder(@"SELECT * FROM BASE_ROLE WHERE F_ID IN(SELECT F_OBJECTID  FROM BASE_USERRELATION WHERE F_USERID = @USERID) ORDER BY F_SortCode asc");
                parameters.Add(DataHelper.CreateParameter("@USERID", userId));
                return await db.GetListSqlAsync<RoleEntity>(sql.ToString(), parameters.ToArray());
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<RoleEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<RoleEntity>(id);
            }
        }
        /// <summary>
        /// 重复验证
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns></returns>
        public async Task<bool> IsExist(Expression<Func<RoleEntity, bool>> condition)
        {
            using (var db = this.Basic)
            {
                return await db.GetCountAsync(condition) > 0;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task Delete(RoleEntity entity)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                await db.DeleteAsync<RoleEntity>(m => m.Id == entity.Id);
                await db.DeleteAsync<UserRelationEntity>(m => m.ObjectId == entity.Id);
                await db.DeleteAsync<AuthorizeEntity>(m => m.ObjectId == entity.Id);
                await db.CommitTransactionAsync();
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(RoleEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(RoleEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }
    }
}
